#!/usr/bin/perl -w
use strict;
use Getopt::Long;

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

my $usage = <<'USAGE';

USAGE:

    re_replace_filter_with_lookup.pl
                --from_regex "([^\t]+)\t"
                --to_regex "\$1\t$match\t"
                --lookup_file file
                --keep_non_matching_lines
                [--match_regex "\$1"]
                [--verbose]
				[--help]

    Iterates through files and filters each line according to the supplied
        regular expression
    The $match variable is the result of lookup what corresponds to
        --match_regex (defaults to $1) after the match
        in the --lookup_file

	Prints to STDOUT

USAGE


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory
my $lookup_file;
my $from_regex;
my $to_regex;
my $match_regex = '$1';
my $keep_non_matching_lines;
my $verbose;
# optional parameters
my $help;
GetOptions(
			'verbose'			=> \$verbose,
			'match_regex=s'		=> \$match_regex,
			'lookup_file=s'		=> \$lookup_file,
			'from_regex=s'		=> \$from_regex,
			'to_regex=s'		=> \$to_regex,
			'keep_non_matching_lines'	=> \$keep_non_matching_lines,
			'help'				=> \$help);

die $usage if ($help);
die $usage unless ($from_regex && defined $to_regex && $lookup_file);
my %lookup;
{
	open LOOKUP_FILE, $lookup_file or die "Error!\nCould not open $lookup_file\n$!\n";
	for (<LOOKUP_FILE>)
	{
		chomp;
		next unless /([^\t]+)\t(.+)/;
		$lookup{$1} = $2;
	}
}

my $match;


my $cmd = <<"CMD";
	for (<>)
	{
		if (/$from_regex/ && exists \$lookup{$match_regex})
		{
			\$match = \$lookup{$match_regex};
			s/$from_regex/{\"$to_regex\"}/e;
			print;
		}
	}
CMD

my $cmd_verbose = <<"CMD";
	for (<>)
	{
		if (/$from_regex/)
		{
			print STDERR '.';
			if (exists \$lookup{$match_regex})
			{
				print STDERR '=';
				\$match = \$lookup{$match_regex};
				print STDERR '>' if s/$from_regex/{\"$to_regex\"}/e;
				print;
			}
		}
	}
CMD

my $cmd_keep_verbose = <<"CMD";
	for (<>)
	{
		if (/$from_regex/)
		{
			print STDERR ".";
			if (exists \$lookup{$match_regex})
			{
				print STDERR "=";
				\$match = \$lookup{$match_regex};
				print STDERR ">" if s/$from_regex/{\"$to_regex\"}/e;
			}
		}
		print;
	}
CMD
my $cmd_keep = <<"CMD";
	for (<>)
	{
		if (/$from_regex/ && exists \$lookup{$match_regex})
		{
			\$match = \$lookup{$match_regex};
			s/$from_regex/{\"$to_regex\"}/e;
		}
		print;
	}
CMD

for (@ARGV)
{
	die "Error:\n\t$_ does not exists.\n" unless -f $_;
}
if ($verbose)
{
	if ($keep_non_matching_lines)
	{
	#	print STDERR $cmd_keep;
		eval $cmd_keep_verbose;
	}
	else
	{
	#	print STDERR $cmd;
		eval $cmd_verbose;
	}}
else
{
	if ($keep_non_matching_lines)
	{
	#	print STDERR $cmd_keep;
		eval $cmd_keep;
	}
	else
	{
	#	print STDERR $cmd;
		eval $cmd ;
	}
}


